using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class UnitSupplySelectionDialog : Form
    {
        private bool speciaActionAdded = false;
        SupplySelectionDialog.supplySelectionItem item;

        private class unitSupplySelectionComboItem
        {
            public string supplyString;
            public int index;
            public override string ToString()
            {
                return supplyString;
            }

            public unitSupplySelectionComboItem(string supplyString,int index)
            {
                this.supplyString = supplyString;
                this.index = index;
            }
        };

        private MapInfo.CountryID GetSpecialActionPayingCountry(MapInfo.CountryID country)
        {
            switch (country)
            {
                case MapInfo.CountryID.Rumania:
                case MapInfo.CountryID.Hungary:
                case MapInfo.CountryID.Finland:
                case MapInfo.CountryID.Bulgaria:
                    return MapInfo.CountryID.Germany;
                case MapInfo.CountryID.FreeFrance:
                    return MapInfo.CountryID.USA;
            }

            return country;
        }

        public UnitSupplySelectionDialog(SupplySelectionDialog.supplySelectionItem item,Dictionary<MapInfo.RegionID, int> seaSupplyRemaining, bool finalSupply, Dictionary<MapInfo.CountryID,int> specialActionsRemaining)
        {
            int index = 0;
            InitializeComponent();
            this.item = item;
            this.RegionLabel.Text = MapInfo.GetRegionName(item.regionID);
            this.supplySelectCombo.Items.Add("No Supply");
            if(finalSupply)
            {
                if(specialActionsRemaining.ContainsKey(GetSpecialActionPayingCountry(item.playingPiece.CountryOwner)))
                {
                    if(specialActionsRemaining[GetSpecialActionPayingCountry(item.playingPiece.CountryOwner)] > 0)
                    {
                        this.supplySelectCombo.Items.Add("Supply by special action");
                        speciaActionAdded = true;
                    }
                }
            }

            if(item.playingPiece.OutOfSupplyState != PlayingPiece.OOSState.OOSMoved || item.inFriendlyControlledRegion)
            {
                foreach (GameController.SeaSupplySolution seaSupplySolution in item.seaRegionPaths)
                {
                    List<SeaRegion> seaRegionPath = seaSupplySolution.seaRegionPath;
                    bool foundEmptySeaSupplyRegion = false;
                    string supplyString = "";
                    if (SupplySelectionDialog.IsAroundTheCapeSeaPath(seaRegionPath))
                    {
                        supplyString = "Around the Cape";
                        if (seaSupplySolution.fromBeachHead)
                        {
                            if (seaSupplyRemaining[MapInfo.RegionID.AtlanticOcean] < 8)
                            {
                                int differenceFromChange = 0;
                                if (item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySea)
                                {
                                    if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains((SeaRegion)MapInfo.GetRegion(MapInfo.RegionID.AtlanticOcean)))
                                    {
                                        if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                            differenceFromChange = 2;
                                        else
                                            differenceFromChange = 1;
                                    }
                                }
                                if (seaSupplyRemaining[MapInfo.RegionID.AtlanticOcean] < (8 - differenceFromChange))
                                    foundEmptySeaSupplyRegion = true;
                            }
                            else if (seaRegionPath.Count > 2 && seaSupplyRemaining[MapInfo.RegionID.MediteraneanSea] < 2)//Must be Med then around the cape
                            {
                                int differenceFromChange = 0;
                                if (item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySea)
                                {
                                    if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains((SeaRegion)MapInfo.GetRegion(MapInfo.RegionID.MediteraneanSea)))
                                    {
                                        if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                            differenceFromChange = 2;
                                        else
                                            differenceFromChange = 1;
                                    }
                                }
                                if (seaSupplyRemaining[MapInfo.RegionID.MediteraneanSea] < (2 - differenceFromChange))
                                    foundEmptySeaSupplyRegion = true;
                            }
                        }
                        else
                        {
                            if (seaSupplyRemaining[MapInfo.RegionID.AtlanticOcean] < 4)
                            {
                                int differenceFromChange = 0;
                                if (item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySea)
                                {
                                    if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains((SeaRegion)MapInfo.GetRegion(MapInfo.RegionID.AtlanticOcean)))
                                    {
                                        if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                            differenceFromChange = 2;
                                        else
                                            differenceFromChange = 1;
                                    }
                                }
                                if (seaSupplyRemaining[MapInfo.RegionID.AtlanticOcean] < (4 - differenceFromChange))
                                    foundEmptySeaSupplyRegion = true;
                            }
                            else if (seaRegionPath.Count > 2 && seaSupplyRemaining[MapInfo.RegionID.MediteraneanSea] <= 0)//Must be Med then around the cape
                            {
                                int differenceFromChange = 0;
                                if (item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySea)
                                {
                                    if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains((SeaRegion)MapInfo.GetRegion(MapInfo.RegionID.MediteraneanSea)))
                                    {
                                        if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                            differenceFromChange = 2;
                                        else
                                            differenceFromChange = 1;
                                    }
                                }
                                if (differenceFromChange <= 0)
                                    foundEmptySeaSupplyRegion = true;
                            }
                        }
                    }
                    else
                    {
                        foreach (SeaRegion seaRegion in seaRegionPath)
                        {

                            supplyString = "->" + MapInfo.GetShortRegionName(seaRegion.RegionID) + supplyString;
                            if (seaSupplySolution.fromBeachHead)
                            {
                                if (seaSupplyRemaining[seaRegion.RegionID] < 2)
                                {
                                    int differenceFromChange = 0;
                                    if (item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySea)
                                    {
                                        if (SupplySelectionDialog.IsAroundTheCapeSeaPath(item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath))
                                        {
                                            if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains(seaRegion))
                                            {
                                                if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                                    differenceFromChange = 8;
                                                else
                                                    differenceFromChange = 4;
                                            }
                                        }
                                        else
                                        {
                                            if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains(seaRegion))
                                            {
                                                if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                                    differenceFromChange = 2;
                                                else
                                                    differenceFromChange = 1;
                                            }
                                        }
                                    }
                                    if (seaSupplyRemaining[seaRegion.RegionID] < (2 - differenceFromChange))
                                    {
                                        foundEmptySeaSupplyRegion = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                if (seaSupplyRemaining[seaRegion.RegionID] <= 0)
                                {
                                    int differenceFromChange = 0;
                                    if (item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySea)
                                    {
                                        if (item.seaRegionPaths[item.selectedSeaPathIndex].seaRegionPath.Contains(seaRegion))
                                        {
                                            if (item.seaRegionPaths[item.selectedSeaPathIndex].fromBeachHead)
                                                differenceFromChange = 2;
                                            else
                                                differenceFromChange = 1;
                                        }
                                    }
                                    if (differenceFromChange <= 0)
                                    {
                                        foundEmptySeaSupplyRegion = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (!foundEmptySeaSupplyRegion)
                    {
                        this.supplySelectCombo.Items.Add(new unitSupplySelectionComboItem(supplyString, index));
                        if (index == item.selectedSeaPathIndex && item.state != SupplySelectionDialog.supplySelectionItemState.unsupplied)
                            this.supplySelectCombo.SelectedIndex = this.supplySelectCombo.Items.Count - 1;
                    }
                    index++;
                }
            }

            if (item.state == SupplySelectionDialog.supplySelectionItemState.unsupplied)
                this.supplySelectCombo.SelectedIndex = 0;
            else if (speciaActionAdded && item.state == SupplySelectionDialog.supplySelectionItemState.suppliedBySpecialAction)
                this.supplySelectCombo.SelectedIndex = 1;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (item.playingPiece is Block)
            {
                PlayingPieceGraphics.PaintBlock(e.Graphics, (Block)item.playingPiece, new Point(10 + PlayingPieceGraphics.GetBlockSize().Height / 2, 10 + PlayingPieceGraphics.GetBlockSize().Width / 2), 1, false);
            }
            else if (item.playingPiece is GroundSupportUnit)
            {
                PlayingPieceGraphics.PaintGSU(e.Graphics, (GroundSupportUnit)item.playingPiece, new Point(10 + PlayingPieceGraphics.GetBlockSize().Height / 2, 10 + PlayingPieceGraphics.GetBlockSize().Width / 2));
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void supplySelectCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.supplySelectCombo.SelectedIndex == 0)
            {
                this.item.playingPiece.OutOfSupplyState = PlayingPiece.OOSState.OOS;
                this.item.state = SupplySelectionDialog.supplySelectionItemState.unsupplied;
            }
            else if (speciaActionAdded && this.supplySelectCombo.SelectedIndex == 1)
            {
                this.item.playingPiece.OutOfSupplyState = PlayingPiece.OOSState.InSupply;
                this.item.state = SupplySelectionDialog.supplySelectionItemState.suppliedBySpecialAction;
            }
            else
            {
                this.item.playingPiece.OutOfSupplyState = PlayingPiece.OOSState.InSupply;
                this.item.selectedSeaPathIndex = ((unitSupplySelectionComboItem)this.supplySelectCombo.Items[this.supplySelectCombo.SelectedIndex]).index;
                this.item.state = SupplySelectionDialog.supplySelectionItemState.suppliedBySea;
            }
        }
    }
}